package capsolver_go

import (
	"context"
	"gitee.com/kaylee595/gkaylee/kerr"
	"github.com/mitchellh/mapstructure"
)

type ReqCreateTaskRecaptcha struct {
	Type              TaskType                 `json:"type" mapstructure:"type"`                                               // required
	WebsiteURL        string                   `json:"websiteURL" mapstructure:"websiteURL"`                                   // required
	WebsiteKey        string                   `json:"websiteKey" mapstructure:"websiteKey"`                                   // required
	PageAction        string                   `json:"pageAction,omitempty" mapstructure:"pageAction,omitempty"`               // optional
	EnterprisePayload map[string]any           `json:"enterprisePayload,omitempty" mapstructure:"enterprisePayload,omitempty"` // optional
	IsInvisible       bool                     `json:"isInvisible,omitempty" mapstructure:"isInvisible,omitempty"`             // optional
	IsSession         bool                     `json:"isSession,omitempty" mapstructure:"isSession,omitempty"`                 // optional
	ApiDomain         string                   `json:"apiDomain,omitempty" mapstructure:"apiDomain,omitempty"`                 // optional
	Proxy             `mapstructure:",squash"` // Optional
}

// CreateTaskRecaptcha
// https://docs.capsolver.com/en/guide/captcha/ReCaptchaV2/
// https://docs.capsolver.com/en/guide/captcha/ReCaptchaV3/
func (s *Solver) CreateTaskRecaptcha(ctx context.Context, i ReqCreateTaskRecaptcha) (string, error) {
	task := make(Task)
	err := mapstructure.Decode(i, &task)
	if err != nil {
		return "", kerr.WrapTraceStack(err)
	}
	retCreateTask, err := s.CreateTask(ctx, task)
	if err != nil {
		return "", kerr.WrapTraceStack(err)
	}
	return retCreateTask.TaskId, nil
}

type RespGetTaskResultRecaptcha struct {
	UserAgent          string `json:"userAgent" mapstructure:"userAgent"`
	CreateTime         int64  `json:"createTime" mapstructure:"createTime"`
	GRecaptchaResponse string `json:"gRecaptchaResponse" mapstructure:"gRecaptchaResponse"`
	RecaptchaCaT       string `json:"recaptcha-ca-t" mapstructure:"recaptcha-ca-t"`
	RecaptchaCaE       string `json:"recaptcha-ca-e" mapstructure:"recaptcha-ca-e"`
}

// GetTaskResultRecaptcha
// https://docs.capsolver.com/en/guide/captcha/ReCaptchaV2/
// https://docs.capsolver.com/en/guide/captcha/ReCaptchaV3/
func (s *Solver) GetTaskResultRecaptcha(ctx context.Context, taskId string) (*RespGetTaskResult[RespGetTaskResultRecaptcha], error) {
	retCreateTask, err := s.GetTaskResult(ctx, taskId)
	if err != nil {
		return nil, kerr.WrapTraceStack(err)
	}
	ret := new(RespGetTaskResult[RespGetTaskResultRecaptcha])
	ret.TaskId = retCreateTask.TaskId
	ret.Status = retCreateTask.Status
	err = mapstructure.Decode(retCreateTask.Solution, &ret.Solution)
	if err != nil {
		return nil, kerr.WrapTraceStack(err)
	}
	return ret, nil
}

type ReqGetTokenRecaptcha = ReqCreateTaskRecaptcha

// GetTokenRecaptcha
// https://docs.capsolver.com/en/guide/captcha/ReCaptchaV2/
// https://docs.capsolver.com/en/guide/captcha/ReCaptchaV3/
func (s *Solver) GetTokenRecaptcha(ctx context.Context, i ReqGetTokenRecaptcha) (*RespGetTaskResult[RespGetTaskResultRecaptcha], error) {
	task := make(Task)
	err := mapstructure.Decode(i, &task)
	if err != nil {
		return nil, kerr.WrapTraceStack(err)
	}
	retToken, err := s.GetToken(ctx, task)
	if err != nil {
		return nil, kerr.WrapTraceStack(err)
	}
	ret := new(RespGetTaskResult[RespGetTaskResultRecaptcha])
	ret.TaskId = retToken.TaskId
	ret.Status = retToken.Status
	err = mapstructure.Decode(retToken.Solution, &ret.Solution)
	if err != nil {
		return nil, kerr.WrapTraceStack(err)
	}
	return ret, nil
}
